﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using BLL;
namespace UI.ModSystem
{
    public partial class DefineSysRole : System.Web.UI.Page
    {
    
        IBizSQL bizSQL = new BizSQL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataSet ds = bizSQL.ExecSQL2("SELECT [SysRoleID], [Name] FROM [Sys_SysRole] WHERE ([State] ='N') ");
                gvDefine.DataSource = ds.Tables[0];
                gvDefine.DataBind();
                DataSet ds2 = bizSQL.ExecSQL2("SELECT [SysRoleID], [Name] FROM [Sys_SysRole] WHERE ([State] ='Y') ");

                gvDefault.DataSource = ds2.Tables[0];
                gvDefault.DataBind();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strName = txtName.Text.Trim();
            string strSysRoleID = txtSysRoleID.Text.Trim();
            if (strSysRoleID.Length<3)
            {
                //弹出提示窗口
                Response.Write("<script>window.alert('编码长度一定要大于3！')</script>");
                return;
            }

            string strInsertSQL = "INSERT INTO Sys_SysRole (SysRoleID,Name,State) VALUES ('" + strSysRoleID + "','" + strName + "','N')";
            bizSQL.ExecSQL(strInsertSQL);
            Response.Redirect("DefineSysRole.aspx");

        }

        protected void btnModify_Click(object sender, EventArgs e)
        {
            string strName = txtName.Text.Trim();
            string strSysRoleID = txtSysRoleID.Text.Trim();
            if (strSysRoleID.Length < 3)
            {
                //弹出提示窗口
                Response.Write("<script>window.alert('编码长度一定要大于3！')</script>");
                return;
            }
            string strUpdateSQL = "UPDATE  Sys_SysRole  SET  Name='" + strName + "' Where SysRoleID='" + strSysRoleID + "'";
            bizSQL.ExecSQL(strUpdateSQL);
            Response.Redirect("DefineSysRole.aspx");
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            string strSysRoleID = txtSysRoleID.Text.Trim();
            if (strSysRoleID.Length > 3)
            {
                bizSQL.ExecSQL("DELETE FROM  Sys_SysRoleMenu  WHERE SysRoleID='" + strSysRoleID + "'");
                bizSQL.ExecSQL("DELETE FROM  Sys_SysRole  WHERE SysRoleID='" + strSysRoleID + "'");
            }
            else
            {
                //弹出提示窗口
                Response.Write("<script>window.alert('编码长度一定要大于3！')</script>");              
            }
            
        }

        protected void btnSetMenu_Click(object sender, EventArgs e)
        {
            string strSysRoleID = txtSysRoleID.Text.Trim();
            if(!string.IsNullOrWhiteSpace(strSysRoleID))
            Response.Redirect("SetSysRoleMenu.aspx?SysRoleID");
            else
                Response.Write("<script>window.alert('先选择一个角色!')</script>");   
        }

        protected void gvDefine_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sysRoleID = "";
            switch (e.CommandName)
            {               
                case "cmdConfigMenu":
                    sysRoleID = e.CommandArgument.ToString();
                    Response.Redirect("~/ModSystem/SetSysRoleMenu.aspx?SysRoleID=" + sysRoleID);
                    break;
                case "cmdConfigRight":
                    sysRoleID = e.CommandArgument.ToString();
                    Response.Redirect("~/ModSystem/RoleMenuRight.aspx?SysRoleID=" + sysRoleID);
                    break;
             
            }
        }

        protected void gvDefault_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sysRoleID = "";
            switch (e.CommandName)
            {
                case "cmdConfigMenu2":
                    sysRoleID = e.CommandArgument.ToString();
                    Response.Redirect("~/ModSystem/SetSysRoleMenu.aspx?SysRoleID=" + sysRoleID);
                    break;
                case "cmdConfigRight2":
                    sysRoleID = e.CommandArgument.ToString();
                    Response.Redirect("~/ModSystem/RoleMenuRight.aspx?SysRoleID=" + sysRoleID);
                    break;

            }
        }
    }
}
